﻿using System;
using System.Windows.Forms;
using Lottery.App.Model;
using Lottery.App.BLL;
using Lottery.App.Utility;

namespace UI
{
    public partial class edit : Form
    {
        public edit()
        {
            InitializeComponent();
        }

        private void edit_Load(object sender, EventArgs e)
        {
            txtQH.Text = String.Format("{0:yyyyMMdd}{1:D3}", DateTime.Now, 1);

            label2.Text = String.Empty;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string qh = txtQH.Text.Trim();
            if (qh.Length != 11)
            {
                MessageBox.Show("期号错误!", "提示");
                return;
            }

            RecordInfo r = new LotteryManager().Get(qh);
            if (r == null)
            {
                MessageBox.Show("期号不存在!请检查要修改的开奖期号!", "提示");
                return;
            }

            label2.Text = "原开奖号:  " + r.Result;
        }



        private void button9_CheckedChange(UI.Control.Button sender, EventArgs e)
        {
            if (!sender.Checked)
            {
                showNumber1.RemoveNumber(sender.Value);
            }
            else
            {
                if (!showNumber1.AddNumber(sender.Value))
                {
                    sender.Checked = false;
                }
            }
        }

        private void showNumber1_NumberChanged()
        {
            foreach (System.Windows.Forms.Control c in p_btns.Controls)
            {
                Control.Button b = c as Control.Button;
                b.Enabled = true;
            }
            if (showNumber1.NumberCount == showNumber1.CanSelectedCount)
            {
                btn_OK.Enabled = true;
                foreach (System.Windows.Forms.Control c in p_btns.Controls)
                {
                    Control.Button b = c as Control.Button;
                    b.Enabled = b.Checked;
                }
            }
            else
            {
                btn_OK.Enabled = false;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            LotteryManager m = new LotteryManager();

            RecordInfo r = m.Get(txtQH.Text.Trim());
            if (r == null)
            {
                MessageBox.Show("期号不存在!请检查要修改的开奖期号!", "提示");
                return;
            }

            btn_OK.Enabled = false;
            btn_clear.Enabled = false;
            btn_exit.Enabled = false;

            string result = this.showNumber1.Value.Trim();

            r.Result = String.Empty;

            int[] array = Utils.ConvertStringToIntArray(result);
            foreach (int item in array)
            {
                r.Result += String.Format("{0:D2} ", item);
            }
            r.Result = r.Result.Trim();
            r.ASC_Result = Utils.Sort(r.Result);
            r.A3 = result.Substring(0, 8);
            r.P3 = Utils.Sort(r.A3);
            r.A1 = result.Substring(0, 2);

            m.Update(r);

            Lottery.App.GlobalData.Refresh();

            btn_clear.Enabled = true;
            btn_exit.Enabled = true;
            MessageBox.Show("数据修改成功!", "提示");
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            showNumber1.ClearNumber();
            foreach (System.Windows.Forms.Control c in p_btns.Controls)
            {
                Control.Button b = c as Control.Button;
                b.Enabled = true;
                b.Checked = false;
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
